<?php

vendor('abstract_object');

class PageObj extends AbstractObject
{
    protected $_controller = null;

    function __construct(&$controller)
    {
        $this->_controller =& $controller;
    }

    protected function &__get_controller()
    {
        return $this->_controller;
    }

    function getController()
    {
        return $this->controller->name;
    }

    function getAction()
    {
        return $this->controller->action;
    }

    function genId()
    {
        static $count = 0;

        return '__sys__'.(++$count);
    }
}

class PageForView extends PageObj
{
    function __construct(&$obj)
    {
        parent::__construct($obj->controller);
    }

}


class Page extends AbstractObject
{
    public static $js = array();
    public static $css = array();
    public static $css_media = array();
    protected static $obj = null;
    public static $jsStrat = array();
    public static $jsEnd = array();

    static function addJS($file)
    {
        $file = str_replace('/', Application::ds(), str_replace('\\', Application::ds(), $file));
        if (! in_array($file, self::$js)) {
            $file = str_replace('/', Application::ds(), str_replace('\\', Application::ds(), $file));
            if (file_exists(Application::jsDir().Application::ds().$file.'.js')) {
                if (Application::ds() != '/') {
                    $file = str_replace(Application::ds(), '/', $file);
                }
                self::$js[] = $file;
            }
        }
    }

    static function addCSS($file, $media=null)
    {
        $file = str_replace('/', Application::ds(), str_replace('\\', Application::ds(), $file));
        if (! in_array($file, self::$css)) {
            if (file_exists(Application::cssDir().Application::ds().$file.'.css')) {
                if (Application::ds() != '/') {
                    $file = str_replace(Application::ds(), '/', $file);
                }
                self::$css[] = $file;
                self::$css_media[] = $media;
            }
        }
    }

    static function insertJSCode($script)
    {
        self::$jsStrat[] = $script;
    }

    static function appendJSCode($script)
    {
        self::$jsEnd[] = $script;
    }

    static function initObj($controller)
    {
        if (is_null(self::$obj)) {
            self::$obj = new PageObj($controller);
        }
    }

    static function &objForView()
    {
        if (! is_null(self::$obj)) {
            $result = new PageForView(self::$obj);
            return $result;
        } else {
            throw new Exception();
        }
    }
}

?>